﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.OleDb;
using DTO;
using DAO;
using BUS;
namespace QL_xxx
{
	/// <summary>
	/// Interaction logic for ThongTinPhong.xaml
	/// </summary>
	public partial class ThongTinPhong : Window
	{
        public event OnSuaPhongThanhCong onSuaPhongThanhCong;
        public event OnLapPhieuThuePhongThanhCong onLapPhieuThueThanhCong;
        public event OnThanhToanThanhCong onThanhToanThanhCong;
        public event OnXoaPhongThanhCong onXoaPhongThanhCong;
        private PhongDTO phong;
        private LoaiPhongDTO loaiPhong;
        bool isEdit;
		public ThongTinPhong(string tenPhong)
		{
			this.InitializeComponent();
            LayThongTinPhong(tenPhong);
            
		}

        private void LayThongTinPhong(string tenPhong)
        {
            phong = PhongBUS.LayDSPhong(-1, tenPhong)[0];
            loaiPhong = LoaiPhongBUS.LayDSLoaiPhong(phong.MaLoaiPhong)[0];
        }
        private void SetValue()
        {
            LayThongTinPhong(phong.TenPhong);
            txtTenPhong.Text = phong.TenPhong;
            txtTrangThai.Text = phong.TrangThai == 0 ? "Trống" : "Có khách";
            cbxLoaiPhong.Text = loaiPhong.TenLoaiPhong;
            txtDonGia.Text = loaiPhong.DonGia.ToString();
            if (phong.TrangThai == 0)//trống
            {
                btnThue.Visibility = Visibility.Visible;
                btnThanhToan.Visibility = Visibility.Hidden;
                btnThemSP.IsEnabled = false;
                btnSua.IsEnabled = true;//khi phong trong moi duoc sua thong tin
            }
            else//có khách
            {
                btnThue.Visibility = Visibility.Hidden;
                btnThanhToan.Visibility = Visibility.Visible;
                btnThemSP.IsEnabled = true;
                btnSua.IsEnabled = false;
            }
        }
        private void LoadCombobox()
        {
            List<LoaiPhongDTO> DSLoaiPhong = LoaiPhongBUS.LayDSLoaiPhong();
            if (DSLoaiPhong != null && DSLoaiPhong.Count > 0)
            {
                foreach (LoaiPhongDTO p in DSLoaiPhong)
                    cbxLoaiPhong.Items.Add(p.TenLoaiPhong);
                cbxLoaiPhong.SelectedItem = loaiPhong.TenLoaiPhong;
            }

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {           
            Edit(false);

            errorTenPhong.Text = "OK";
            errorTenPhong.Foreground = Brushes.Green;

            SetValue();
            LoadCombobox();

           
        }
        private void Edit(bool _isEdit)// ha de enable/disenable controll cho viec xem va sua
        {
            isEdit = _isEdit;
            //nhom control enable khi edit
            txtTenPhong.IsEnabled = _isEdit;
            cbxLoaiPhong.IsEnabled = _isEdit;
            btnLuu.IsEnabled = _isEdit;
            
            //nhom control enable khi xem
            btnThue.IsEnabled = !_isEdit;
            btnXoa.IsEnabled = !_isEdit;
            if (_isEdit)
            {
                btnSua.Visibility = Visibility.Hidden;
                btnHuy.Visibility = Visibility.Visible;
                errorTenPhong.Visibility = Visibility.Visible;
            }
            else
            {
                btnSua.Visibility = Visibility.Visible;
                btnHuy.Visibility = Visibility.Hidden;
                errorTenPhong.Visibility = Visibility.Hidden;
            }
        }

        private void btnLuu_Click(object sender, RoutedEventArgs e)
        {
            PhongDTO p = phong.Clone();
            p.TenPhong = txtTenPhong.Text;
            p.MaLoaiPhong = LoaiPhongBUS.LayDSLoaiPhong(-1, cbxLoaiPhong.Text)[0].MaLoaiPhong;
            int kq = PhongBUS.SuaPhong(p);
            switch (kq)
            {
                case PhongBUS.trungTenPhong:
                    {
                        MessageBox.Show("Tên phòng này đã tồn tại, hãy nhập tên khác", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                        break;
                    }
                case PhongBUS.suaPhongLoi:
                    {
                        MessageBox.Show("Lỗi chưa xác định, vui lòng liên hệ nhà cung cấp.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                        break;
                    }
                case PhongBUS.suaPhongOK:
                    {
                        MessageBox.Show("Đã sửa.", "Sửa thành công", MessageBoxButton.OK, MessageBoxImage.Information);
                        onSuaPhongThanhCong();
                        phong = p;
                        SetValue();
                        Edit(false);
                        
                        break;
                    }
            }
        }

        private void btnSua_Click(object sender, RoutedEventArgs e)
        {
            if(!isEdit)
                Edit(true);
        }

        private void btnHuy_Click(object sender, RoutedEventArgs e)
        {
            if (isEdit)
            {
                Edit(false);
                SetValue();
            }
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBoxResult.Yes == MessageBox.Show("Bạn có chắc muốn thoát, thông tin sẽ không được lưu!", "Thông báo.", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No))
                this.Close();
        }

        private void txtTenPhong_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtTenPhong.Text.Trim().Length == 0)
            {
                errorTenPhong.Foreground = Brushes.Red;
                errorTenPhong.Text = "Bạn phải điền giá vào đây.";
            }
            else
            {
                errorTenPhong.Text = "OK";
                errorTenPhong.Foreground = Brushes.Green;
            }
        }

        private void errorTenPhong_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (isEdit)
            {
                if (string.Compare(txtTenPhong.Text, "OK") == 0)
                    btnLuu.IsEnabled = true;
                else
                    btnLuu.IsEnabled = false;
            }
        }

        private void btnThue_Click(object sender, RoutedEventArgs e)
        {
            LapPhieuThuePhong phieuThue = new LapPhieuThuePhong(phong.TenPhong);
            phieuThue.onLapPhieuThueThanhCong += new OnLapPhieuThuePhongThanhCong(phieuThue_onLapPhieuThueThanhCong);
            phieuThue.ShowDialog();
        }

        private void phieuThue_onLapPhieuThueThanhCong()
        {
            LayThongTinPhong(phong.TenPhong);
            SetValue();
            onLapPhieuThueThanhCong();
        }

        private void btnThanhToan_Click(object sender, RoutedEventArgs e)
        {
            PhieuThuePhongDTO phieuThue = PhieuThuePhongBUS.LayDSPhieuThuePhong(phong.TrangThai)[0];
            KetQuaThanhToan kqtt = new KetQuaThanhToan(phieuThue.MaPhieuThuePhong,phong.MaPhong);
            kqtt.onThanhToanThanhCong += new OnThanhToanThanhCong(kqtt_onThanhToanThanhCong);
            kqtt.ShowDialog();
        }

        void kqtt_onThanhToanThanhCong()
        {
            LayThongTinPhong(phong.TenPhong);
            SetValue();
            onThanhToanThanhCong();
        }

        private void btnThemSP_Click(object sender, RoutedEventArgs e)
        {
            ThemChiTietPhongSanPham ctpsp = new ThemChiTietPhongSanPham(phong.MaPhong);
            ctpsp.ShowDialog();
        }

        private void btnXoa_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBoxResult.Yes == MessageBox.Show("Bạn có chắc xóa, xóa phòng sẽ dẫn tới xóa hết các thông tin liên quan!", "Cẩn thận", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No))
            {
                if (PhongBUS.XoaPhong(phong.MaPhong))
                {
                    MessageBox.Show("Xong");
                    onXoaPhongThanhCong();
                }
                else
                    MessageBox.Show("Lỗi chưa xác định, xin liên hệ nhà cung cấp");
            }
        }
	}
}